<template>
    <div class="back">
        <el-card>
            <el-form ref="form" :model="form" label-width="80px" :rules="rules">
                <el-input
                    v-model="form.username"
                    placeholder="请输入用户名"
                    size="medium"
                    prefix-icon="el-icon-user"
                ></el-input>
                <el-input
                    v-model="form.password"
                    placeholder="请输入密码"
                    show-password
                    size="medium"
                    prefix-icon="el-icon-lock"
                ></el-input>
                <el-button type="primary" @click="onSubmit" style="width: 100%" size="medium">登录</el-button>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import { login } from '@/api/auth'
import router from '@/router/index.js'
import { createNamespacedHelpers } from 'vuex'
const { mapActions } = createNamespacedHelpers('user')
export default {
    data() {
        return {
            form: {},
            rules: {
                name: { required: true, message: '请输入用户名', trigger: 'blur' },
                password: { required: true, message: '请输入密码', trigger: 'blur' }
            }
        }
    },
    methods: {
        ...mapActions(['setUserInfo']),
        onSubmit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    login(this.form)
                        .then((data) => {
                            this.setUserInfo(data.data).then((v) => {
                                router.push({ path: '/index' })
                            })
                        })
                        .catch((data) => {
                            this.$message(data.msg)
                        })
                } else {
                    return false
                }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.el-card {
    width: 350px;
}
.back {
    display: flex;
    justify-content: center;
    height: 100%;
    background-image: url('@/assets/images/vue.png');
    background-repeat: no-repeat;
    align-items: center;
    background-attachment: fixed;
    background-size: cover;
}
.el-input,
.el-button--primary {
    margin: 10px 0;
}
</style>
